# coding=utf-8
# !/usr/bin/python

# 线程池
from concurrent.futures import ThreadPoolExecutor

# 导入requests库
import requests
# 导入文件操作库
import os
import shutil
# bs4全名BeautifulSoup，是编写python爬虫常用库之一，主要用来解析html标签。
import bs4
from bs4 import BeautifulSoup
# 基础类库
import sys
import random
# Python 3.x 解决中文编码问题
import importlib

importlib.reload(sys)

random_headers = [
    "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 "
    "Safari/537.36",
    "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 "
    "Safari/537.75.14",
    "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; Trident/6.0)",
    'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11',
    'Opera/9.25 (Windows NT 5.1; U; en)',
    'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
    'Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.5 (like Gecko) (Kubuntu)',
    'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12',
    'Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/1.2.9',
    "Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.7 (KHTML, like Gecko) Ubuntu/11.04 Chromium/16.0.912.77 "
    "Chrome/16.0.912.77 Safari/535.7",
    "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:10.0) Gecko/20100101 Firefox/10.0",
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36'
]

# 给请求指定一个请求头来模拟chrome浏览器
global headers
headers = {'User-Agent': random.choice(random_headers)}
# 爬图地址
domain = 'http://zz.ke.com'
# 定义存储位置
global base_path
base_path = '/Users/liujun/Documents/mzitu'


# 创建文件夹
def createFile(file_path):
    if os.path.exists(file_path) is False:
        os.makedirs(file_path)
    # 切换路径至上面创建的文件夹
    os.chdir(file_path)


# 下载文件
def download(url):
    global headers
    global base_path
    res_page = requests.get(url, headers=headers)
    # 解析html
    soup_page = BeautifulSoup(res_page.text, 'html.parser')
    # 获取页面的栏目地址
    all_a = soup_page.find('div', class_='postlist').find_all('a', target='_blank')
    count = 0
    for a in all_a:
        count = count + 1
        # 获取真实详情地址
        if (count % 2) == 0:
            # 提取href
            detail_url = a.attrs['href']
            print('套图地址：' + detail_url)
            res_detail = requests.get(detail_url, headers=headers)
            soup_detail = BeautifulSoup(res_detail.text, 'html.parser')
            try:
                # 获取套图的最大数量
                pic_max = soup_detail.find('div', class_='pagenavi').find_all('span')[6].text
                print("套图数量：" + pic_max)
                main_title = soup_detail.find('h2', class_='main-title').text
                img_path = base_path + '/' + main_title
                createFile(img_path)
                for i in range(1, int(pic_max) + 1):
                    # 单个图片地址
                    img_url = detail_url + "/" + str(i)
                    res_page_img = requests.get(img_url, headers=headers)
                    soup_page_img = BeautifulSoup(res_page_img.text, "html.parser")
                    main_image = soup_page_img.find('div', class_='main-image').find('img')
                    if isinstance(main_image, bs4.element.Tag):
                        # 提取src
                        url = main_image.attrs['src']
                        array = url.split('/')
                        file_name = array[len(array)-1]
                        # print(file_name)
                        # 防盗链加入Referer
                        headers['Referer'] = domain
                        img = requests.get(url, headers=headers)
                        # print('开始保存图片')
                        f = open(file_name, 'ab')
                        f.write(img.content)
                        # print(file_name, '图片保存成功！')
                        f.close()
                        shutil.move(file_name, img_path + '/' + file_name)
            except Exception as e:
                print(e)


# 主方法
def main():
    res = requests.get(domain, headers=headers)
    # 使用自带的html.parser解析
    soup = BeautifulSoup(res.text, 'html.parser')
    # 创建文件夹
    createFile(base_path)
    # 获取首页总页数
    pages = soup.find('div', class_='nav-links').find_all('a')[3].text
    print("总页数:"+pages)
    with ThreadPoolExecutor(max_workers=5) as t:
        for i in range(1, int(pages) + 1):
            # 获取每页的URL地址
            if i == 1:
                url = domain
            else:
                url = domain + '/page/' + str(i)
            # 下载每页的图片
            t.submit(download, url)


if __name__ == '__main__':
    main()
